﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace CardLogistics.Web.Controls
{
    /// <summary>
    /// HdAppro 的摘要说明
    /// </summary>
    public class HdAppro : IHttpHandler
    {

        private static readonly CardLogistics.DAL.Approval.ApprovalInfoData DAL = new DAL.Approval.ApprovalInfoData();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string InfoId = context.Request.QueryString["InfoId"];//审批信息主键
            string UserId = context.Request.QueryString["UserId"];//审批人id
            string APPCONTENT = context.Server.HtmlDecode(context.Request.QueryString["APPCONTENT"]);//审批意见 
            string appStatus = context.Request.QueryString["appStatus"];//审批状态 1=通过 2=不通过 3=驳回
            string infoType = context.Request.QueryString["InfoType"];//审批类型
            if (!string.IsNullOrEmpty(InfoId))
            {
                string sql = " UPDATE APPROVALINFO SET APPSTATUS=" + appStatus + ",APPCONTENT='" + APPCONTENT + "',APPDATE=sysdate ";
                sql += " WHERE USERID='" + UserId + "' and INFOID='" + InfoId + "'";//更新审批信息
                int result = CardLogistics.DAL.DbHelperSQL.ExecuteSql(sql);
                if (result > 0)
                {
                    DataTable dt = DAL.GetInfoByInfoId(InfoId);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        //0 未提交 1 待审核 2 审核中 3 驳回 4 审核不通过 5审核通过
                        int infoStatus = 0;
                        //审批结果 APPSTATUS  0 =未审批、1=审批通过、2=审批未通过、3=驳回
                        //if (dt.Select(" APPSTATUS<>1 ").Length == 0)//全部为1表示审核通过(修改前的数据)
                        if (dt.Select(" APPSTATUS=1 ").Length == dt.Rows.Count)//全部为1表示审核通过(修改后的数据)
                            infoStatus = 5;//审批通过
                        else if (dt.Select(" APPSTATUS=3 ").Length > 0)//若有其中一条驳回则状态为驳回
                            infoStatus = 3;//驳回
                        else if (dt.Select(" APPSTATUS=2 ").Length > 0)//若有其中一条不通过状态为审核不通过
                            infoStatus = 4;//审核不通过
                        else if (dt.Select(" APPSTATUS>0 ").Length > 0 && dt.Select(" APPSTATUS=0 ").Length > 0)//审核中
                            infoStatus = 2;
                        string sqlStatus = string.Empty;
                        if (infoStatus > 0)
                        {
                            switch (infoType)
                            {
                                case "0"://采购需求
                                    sqlStatus = " Update PURCHASE_DEMAND set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    break;
                                case "1"://采购合同
                                    sqlStatus = " Update PURCHASE_CONTRACT set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    break;
                                case "2"://卡片订货单
                                    sqlStatus = " Update ORDERING_CARDINFO set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    break;
                                case "3"://卡片验收入库
                                    sqlStatus = " Update STORAGE_CHECK_CARD set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    if (infoStatus == 4)//审核不通过，删除卡详细流水和缺卡记录表数据
                                    {
                                        sqlStatus += "delete from cardrecord where RKId='" + InfoId + "';";
                                        sqlStatus += "delete from cardrecord_lack where RKId='" + InfoId + "';";
                                    }
                                    break;
                                case "4"://卡片需求单
                                    sqlStatus = " Update CARDMAKING_CARDDEMAND set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    break;
                                case "5"://半成品/成品入库
                                    sqlStatus = " Update PUTINCARD set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    if (infoStatus == 4)//审核不通过，恢复卡详细流水记录表原始状态
                                    {
                                        sqlStatus += "update CARDRECORD set BCPRUKUSJ=null,BCPRUKUID=null where BCPRUKUID='" + InfoId + "';";
                                    }
                                    break;
                                case "6"://卡片发放需求
                                    sqlStatus = " Update PUTOUTDEMAND set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    break;
                                case "7"://半成品/成品卡申领
                                    sqlStatus = " Update PUTOUTAPPLY set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    break;
                                case "8"://半成品/成品卡出库
                                    sqlStatus = " Update PUTOUTCARD set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    if (infoStatus == 4)//审核不通过，恢复卡详细流水记录表原始状态
                                    {
                                        sqlStatus += "update CARDRECORD set CPCHUSJ=null,CPCHUID=null where CPCHUID='" + InfoId + "';";
                                    }
                                    break;
                                case "9"://半成品/成品卡发放
                                    sqlStatus = " Update PUTOUTSENDCARD set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    if (infoStatus == 4)//审核不通过，恢复卡详细流水记录表原始状态
                                    {
                                        sqlStatus += "update CARDRECORD set CPFAFANGSJ=null,CPFAFANGID=null where CPFAFANGID='" + InfoId + "';";
                                    }
                                    break;
                                case "10"://白卡出库
                                    sqlStatus = " Update CARDMAKING_CARDOUTSTORAGE set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    if (infoStatus == 4)//审核不通过，恢复卡详细流水记录表原始状态
                                    {
                                        sqlStatus += "update CARDRECORD set BKCHUKUSJ=null,BKCHUKUID=null where BKCHUKUID='" + InfoId + "';";
                                    }
                                    break;
                                case "11"://制卡坏卡
                                    sqlStatus = " Update CARDRECOVERY_MAKINGBAD set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    if (infoStatus == 4)//审核不通过，恢复卡详细流水记录表原始状态
                                    {
                                        sqlStatus += "update CARDRECORD set ZHIKAHKSJ=null,ZHIKAHKID=null where ZHIKAHKID='" + InfoId + "';";
                                    }
                                    break;
                                case "12"://未售问题卡回收
                                    sqlStatus = " Update NOTSALECARD set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    if (infoStatus == 4)//审核不通过，恢复卡详细流水记录表原始状态
                                    {
                                        sqlStatus += "update CARDRECORD set WSWTKSJ=null,WSWTKID=null where WSWTKID='" + InfoId + "';";
                                    }
                                    break;
                                case "13"://报废入库
                                    sqlStatus = " Update CARDRECOVERY_SCRAP set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    if (infoStatus == 4)//审核不通过，恢复卡详细流水记录表原始状态
                                    {
                                        sqlStatus += "update CARDRECORD set BFRKSJ=null,BFRKID=null where BFRKID='" + InfoId + "';";
                                    }
                                    break;
                                case "14"://卡片退货
                                    sqlStatus = " Update CARDRECOVERY_RETURNCARD set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    break;
                                case "15"://调拨入库
                                    sqlStatus = " Update PUTINALLOCATION set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    break;
                                case "16"://调拨出库
                                    sqlStatus = " Update PUTOUTALLOCATION set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    break;
                                case "17"://离盒操作
                                    sqlStatus = " Update CARDMAKING_CARDSEPARATE set ISSUBMIT=" + infoStatus + " where ID='" + InfoId + "';";
                                    break;
                            }
                        }
                        if (!string.IsNullOrEmpty(sqlStatus))
                        {
                            //0 未提交 1 待审核 2 审核中 3 驳回 4 审核不通过 5审核通过
                            CardLogistics.DAL.DbHelperSQL.ExecuteSql("begin " + sqlStatus + " end;");//更新审批内容状态 
                        }
                    }
                    context.Response.Write("ok");
                }
                else
                    context.Response.Write("err");
            }
            else
                context.Response.Write("noinfo");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}